<template>
  <div class="app-container">
    <div class="customContainer ">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <dj-store-select :category="546" :needItem="true" :defaultStore="formInline.store_id" @change="selectStoreChanged" />
        <el-form-item>
          <el-date-picker v-model="pickerDate" value-format="yyyy-MM-dd" type="daterange" align="right" unlink-panels range-separator="-"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <dj-simple-table :columns="tableColumns" :data="listData" :global="global" :loading="listLoading" />
    </div>
  </div>
</template>
<script>
import { getSummary } from '@/api/report/store_stock_in';
import { DjSimpleTable } from '@/components/table';
import { DjStoreSelect } from '@/components/store';
import { pickerOptionsF, pickerDateDot, optionMaxTime, getDateString } from '@/utils';
export default {
  components: {
    'dj-simple-table': DjSimpleTable,
    'dj-store-select': DjStoreSelect
  },
  data() {
    return {
      global: {
        store_name: '',
        start_time: '',
        end_time: ''
      },
      tableColumns: [
        {
          label: '门店名称',
          dtcol: 'store_name'
        },
        {
          label: '供应商采购入库金额',
          dtcol: 'supplier_money',
          isnum: true,
          link: 'report/stock_in_purchase_form'
        },
        {
          label: '中央仓采购入库金额',
          dtcol: 'house_money',
          isnum: true,
          link: 'report/stock_in_purchase_form'
        },
        {
          label: '其他仓调入金额',
          dtcol: 'other_money',
          isnum: true,
          link: 'report/stock_allocate_summary_form',
        },
        {
          label: '合计',
          dtcol: 'total',
          isnum: true
        }
      ],
      listData: [], // 对象数组元素中的对象含有store_id属性
      listLoading: false,
      formInline: {
        store_id: this.$store.getters.optionId,
        start_time: '',
        end_time: '',
        look_del: '0'
      },
      pickerOptions: pickerOptionsF(-1),
      pickerDate: pickerDateDot(-1,-1)
    };
  },
  watch: {
    pickerDate: function() {
      this.global.start_time = this.pickerDate[0];
      this.global.end_time = this.pickerDate[1];
    }
  }, 
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.beforeComingIn();
    });
  },
  methods: { 
    beforeComingIn() {
      if (this.pickerDate.length > 1) {
        this.global.start_time = this.pickerDate[0];
        this.global.end_time = this.pickerDate[1];
      }
      if (this.$route.query.routerflag) {
        // console.log(this.$route.query, 'zst1119 beforeComingIn route.query');
        let col = this.$route.query.col;
        // 本月第一天至昨天
        let fist_day = getDateString('this_month_first_day', this.$route.query.row.time);
        let date = new Date(this.$route.query.row.time);
        let now = new Date();
        let yesterday;
        if (date.getMonth() == now.getMonth()) {
          yesterday = getDateString('yesterday');
        } else {
          yesterday = getDateString('this_month_last_day', this.$route.query.row.time);
        }
        this.pickerDate = [fist_day, yesterday];
        this.formInline.store_id = this.$route.query.row.store_id;
        // this.searchClick();
      }
      //  else {
      //   if (!this.listData || this.listData.length == 0) {
      //     this.searchClick();
      //   }
      // }
    },
    selectStoreChanged(obj, item) {
      for (let key in obj) this.formInline[key] = obj[key];
      this.global.store_name = item.name;
      this.searchClick();
    },
    // 搜索
    searchClick() {
      optionMaxTime(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.start_time = this.pickerDate ? this.pickerDate[0] : '';
        this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
        this.fetchData();
      });
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      getSummary(this.formInline)
        .then(response => {
          // console.log(response, 'zst1120 getSummary');
          if (response.code == 1) {
            response = response.message;
            response.store_id = this.formInline.store_id;
            response.store_name = this.global.store_name;
            response.total = response.supplier_money * 1 + response.house_money * 1 + response.other_money * 1;
            this.listData = [response];
          } else {
            this.listData = [];
          } 
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
        });
    }
  }
};
</script>